/** *添加部门或成员* */
var v_url="/lvds/tagInfo/getMemberAndPartyJson";
var setting = {
	data : {
		simpleData : {
			enable : true,
			idKey : "id",
			pIdKey : "pId"
		}
	},
	edit : {
		enable : false,
		drag : {// 禁止拖拽
			isCopy : false,
			isMove : false
		}
	},
	async : {
		enable : true,
		url : v_url,
		autoParam : [ "id", "name" ]
	},
	callback : {
		onClick : zTreeOnClick,// 点击事件
	}
};

var treeObj;
/*$(document).ready(function() {
	// 加载机构成员
	$.fn.zTree.init($("#treeDemo"), setting);
	treeObj = $.fn.zTree.getZTreeObj("treeDemo");
});*/
// 绑定新增按钮事件
$("#addSysUserInfo").click(function() {
	/*// 显示前 清空上次选择的内容
	$("div[class='label_right_col'] ul").children().remove();
	$("div[id='hidden_content']").children().remove();*/
	// 显示选择页面
	$("#addSysUserInfoWin").modal('show');


});
var partyIds = ""; var partyNames = "";
var memberIds = ""; var memberNames = "";
// 单击节点时选择部门或机构 同时右边显示对应的名称
function zTreeOnClick(e, treeId, treeNode) {
	var data_type = treeNode.data_type;
	if (treeNode.checked) {// 如果节点已被选中 则点击后状态修改为 未选中 删除相应的li
		treeNode.checked = false;
		treeObj.updateNode(treeNode);
		// 取消后移除对勾
		$("#" + treeNode.tId + "_selected").remove();
		// 取消后移除右边显示框对应内容
		$("li[id='" + treeNode.id + "']").remove();
		// 移除对应id
		$("div[id='hidden_content'] input[id='input_id_" + treeNode.id + "']")
				.remove();
		if(data_type=="party"){
			//取消选中部门同时删除partyNames中对应的部门name
			partyNames = partyNames.replace(""+treeNode.name+",", "") ;
			//取消选中部门同时删除partyIds中对应的部门id
			partyIds = partyIds.replace(""+treeNode.id+"|", "") ;
		}
		else if(data_type=="member"){
			//取消选中成员同时删除memberNames中对应的成员name
			memberNames = memberNames.replace(""+treeNode.name+",", "") ;
			//取消选中成员同时删除memberIds中对应的成员id
			memberIds = memberIds.replace(""+treeNode.id+"|", "") ;
		}
	} else {// 如果节点未被选中则 点击后状态修改为 已选中
		treeObj.selectNode(treeNode);
		treeNode.checked = true;
		treeObj.updateNode(treeNode);
		// 选中后添加对勾
		$("#" + treeNode.tId + "_a")
				.append(
						"<span id=\""
								+ treeNode.tId
								+ "_selected\" class=\"label_right_col_a_span\">√</span>");
		// 选中后在右边显示框显示对应内容
		if (treeNode.data_type == "party") {
			$("div[class='label_right_col'] ul")
					.append(
							"<li class=\"label_right_col_li\" id=\""
									+ treeNode.id
									+ "\"><img class = \"partyImg\" src=\"../images/icons/wx/icon_folder_blue.png\" ><span class=\"selectText\">"
									+ treeNode.name
									+ "</span><a href=\"javascript:void(0);\" onclick='onDelSpan("
									+ JSON.stringify(treeNode)
									+ ")' class=\"label_right_col_a_delete\"><span class=\"label_right_col_a_delete_img\"></span></a></li>");
			//记录选择部门name
			partyNames = partyNames + treeNode.name+",";
			//记录选择部门id
			partyIds = partyIds + treeNode.id + "|";
		} else if (treeNode.data_type == "member") {
			$("div[class='label_right_col'] ul")
					.append(
							"<li class=\"label_right_col_li\" id=\""
									+ treeNode.id
									+ "\"><img class = \"memberImg\" src=\"../images/icons/wx/icon_people_blue.png\" ><span class=\"selectText\">"
									+ treeNode.name
									+ "</span><a href=\"javascript:void(0);\" onclick='onDelSpan("
									+ JSON.stringify(treeNode)
									+ ")' class=\"label_right_col_a_delete\"><span class=\"label_right_col_a_delete_img\"></span></a></li>");
			//记录选择部门name
			memberNames = memberNames + treeNode.name+",";
			//记录选择部门id
			memberIds = memberIds + treeNode.id + "|";
		}
		// 储存字段规范 id|data_type
		$("div[id='hidden_content']").append(
				"<input type=\"text\" id=\"input_id_" + treeNode.id
						+ "\" name=\"tag_content\" value=\"" + treeNode.id
						+ "|" + treeNode.data_type + "\"   \/>");
	}
};

function onDelSpan(treeNode) {
	var treeNode_temp;// 声明树某个节点对象
	// 取消后移除对勾
	$("#" + treeNode.tId + "_selected").remove();
	// 取消后移除右边显示框对应内容
	$("li[id='" + treeNode.id + "']").remove();
	var data_type = treeNode.data_type;
	// 由当前树对象查找TId获取当前节点 改变选中状态 防止点击事件调用不准确
	if(data_type=="party"){
		treeNode_temp = treeObj.getNodeByTId(treeNode.tId);
		treeNode_temp.checked = false;
		treeObj.updateNode(treeNode_temp);
		//取消选中部门同时删除partyNames中对应的部门name
		partyNames = partyNames.replace(""+treeNode.name+",", "") ;
		//取消选中部门同时删除partyIds中对应的部门id
		partyIds = partyIds.replace(""+treeNode.id+"|", "") ;
	}
	else if(data_type=="member"){
		treeNode_temp = treeObj.getNodeByTId(treeNode.tId);
		treeNode_temp.checked = false;
		treeObj.updateNode(treeNode_temp);
		//取消选中成员同时删除memberNames中对应的成员name
		memberNames = memberNames.replace(""+treeNode.name+",", "") ;
		//取消选中成员同时删除memberIds中对应的成员id
		memberIds = memberIds.replace(""+treeNode.id+"|", "") ;
	}
	// 移除对应id
	$("div[id='hidden_content'] input[id='input_id_" + treeNode.id + "']")
			.remove();
}
//确定按钮后执行操作
$("#btnSubmit").click(function() {
	var receives = "";
	//将选择的id赋值给相应的输入框
	$("#toParty").val(partyIds.substring(0, partyIds.lastIndexOf("\|")));
	$("#toUsers").val(memberIds.substring(0, memberIds.lastIndexOf("\|")));
	//组合部门、成员
	//组合标签、部门、成员
	if(partyIds!=""){
		 receives= "部门：" + partyNames;
	}
	else{
		if(partyIds!="") receives = "部门：" + partyNames;
	}
	if(receives!=""&&memberIds!=""){
		receives = receives+"\r\n"+"成员：" + memberNames;
	}
	else{
		if(receives!="") receives = receives;
		if(memberIds!="") receives = "成员：" + memberNames;
	}
	$("#receives").val(receives);
	$("#addSysUserInfoWin").modal('hide');
	
});


